home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / xedt.c < prev   
Encoding:
C/C++ Source or Header  |  1992-11-29  |  21.3 KB  |  1,076 lines

  1. #include "all.h"
  2. #include <errno.h>
  3. #include <math.h>
  4. #ifdef __TURBOC__
  5. #define HLINECHAR 205
  6. #include "bios.h"
  7. #include "conio.h"
  8. #else
  9. #define ENOFILE 999
  10. #define HLINECHAR '-'
  11. #include "vaxconio.h"
  12. #endif
  13. #include "edt.h"
  14. int vax_edt(char *s);
  15. int delay(int i);
  16. extern int gle_debug;
  17. int gle_speed;
  18. int gle_nspeed;
  19. int trace_on;
  20. #define true (!false)
  21. #define false 0
  22. #define dbg if (gle_debug>0)
  23. long mem_total(void);
  24. int pick_file(char *r, char *w);
  25. typedef char (*TEXT)[];
  26. int scr_blackwhite;
  27. int scr_left(int i);
  28. int scr_right(int i);
  29. int scr_norm(void);
  30. int scr_inv(void);
  31. int scr_grey(void);
  32. int text_search(void);
  33. int text_deleol(void);
  34. int text_undeleol(void);
  35. int scr_savexy(void);
  36. int scr_restorexy(void);
  37. int scr_gets(char *s);
  38. int text_showerror(void);
  39. int text_findnext(void);
  40. int text_changed(void);
  41. int read_command(char *ans, char *ques);
  42. int read_str(char *s);
  43. int text_load(char *fname);
  44. int text_free(void);
  45. int do_help(char *s);
  46. int int_edt(char *fname);
  47. int vinsert(int y, char *s);
  48. int vdelete(int y);
  49. int menu(void);
  50. int gle_redraw(void);
  51. int scrinsert(int y);
  52. int scrdeleteline(int y);
  53. int text_save(void) ;
  54. int text_eol(void);
  55. int text_delright(void);
  56. int text_delete(void);
  57. int text_undelete(void);
  58. int text_deleteline(void);
  59. int text_left(void);
  60. int text_right(void);
  61. int text_up(void);
  62. int text_down(void);
  63. int text_return(void);
  64. int text_refresh();
  65. int text_putstr(char *s);
  66. int text_setwindow();
  67. int text_saveas(void);
  68. int text_move(int x,int y);
  69. int scr_tab(char *s, int x);
  70. int scr_negtab(char *s, int x, int *fpos, int *scrx);
  71. int fner(char *s);
  72. int fner_clear(void);
  73. int text_setwindow(void);
  74. int text_refresh(void);
  75. int text_main(void);
  76. int text_inkey(void);
  77. int text_scroll(void);
  78. int text_scroll_up(void);
  79. int text_scroll_down(void);
  80. int text_select(void);
  81. int text_cut(void);
  82. int text_paste(void);
  83. int restofline(int y,int x);
  84. int normal_key(int c);
  85. int lineset(int i,char *ss);
  86. int lineinsert(int y, char *ss);
  87. int textinsert(int y);
  88. char *sline(int i);
  89. char *line(int i);
  90. int ncpy(char *d, char *s, int n);
  91. /*------------ GLOBAL VARIABLES --------------*/
  92. extern long gtotalmem;
  93. int max_ngtxt;
  94. extern int ngtxt;
  95. extern char *(*gtxt)[];   /* gtxt is a pointer to an array of poiter to char */
  96. char *(*ctxt)[];    /* cut buffer */
  97. int ncut;
  98. int iserr;
  99. int w_top=0,w_bot=0,w_len,w_cl,w_cx;
  100. #define MAXLINES (max_ngtxt-10)
  101. extern int ngtxt;
  102. int move_pos,onmove;
  103. int ii;
  104. char gbuff[255];
  105. char gbuff2[255];
  106. int selx,sely;
  107. extern char input_file[];
  108. int changed;
  109. int scr_isblackwhite(void);
  110. extern int netxt;    /* errors which occured */
  111. extern char *(*etxt)[];
  112. /*---------------------------------------------------------------------------*/
  113. /* copy gtxt, ngtxt   to local paramters */
  114. int tfx,tfy;
  115. extern int gotfile;
  116. int_edt(char *fname)
  117. {
  118.     int i,j;
  119.  
  120.     tfx = 1;
  121.     tfy = 1;
  122.     w_len = 20;
  123.     scr_blackwhite = scr_isblackwhite();
  124.     if (!gotfile) {
  125.         { char infile[80]; char wildcard[80];
  126.         strcpy(wildcard,"*.*");
  127.         if (pick_file(infile,wildcard)) strcpy(infile,"test.gle");
  128.         text_load(infile);
  129.         }
  130.     }
  131.     gotfile=true;
  132.     text_setwindow();
  133.     text_refresh();
  134.     text_main();
  135. }
  136. vinsert(int y,char *ss)
  137. {
  138.     int i;
  139.     if (ngtxt>(max_ngtxt-50)) text_expand(2*(ngtxt+50));
  140.     if (ngtxt>MAXLINES) {fner("too many lines in file "); return;}
  141.     ngtxt++;
  142.     for (i=ngtxt;i>y;i--)
  143.         (*gtxt)[i] = (*gtxt)[i-1];
  144.     (*gtxt)[y] = sdup(ss);
  145. }
  146. lineinsert(int y,char *ss)
  147. {
  148.     vinsert(y,ss);
  149.     scrinsert(y);
  150. }
  151.  
  152. vdelete(int y)
  153. {
  154.     int i;
  155.     if (ngtxt==0) {fner("too few lines in file "); return;}
  156.     ngtxt--;
  157.     myfrees((*gtxt)[y],"vdelete");
  158.     for (i=y;i<=ngtxt;i++)
  159.         (*gtxt)[i] = (*gtxt)[i+1];
  160. }
  161. linedelete(int y)
  162. {
  163.     vdelete(y);
  164.     scrdeleteline(y);
  165. }
  166. lineset(int i,char *ss)
  167. {
  168.     char *zzz;
  169.     int same;
  170.     char *d=(*gtxt)[i];
  171.     char *s=ss;
  172.     if (d==0) gle_abort("linset zero\n");
  173.     for (;*d!=0 && *s!=0 && *d==*s ;s++,d++) ;
  174.     same = s-ss;
  175.     myfrees((*gtxt)[i],"Linset");
  176.     zzz = sdup(ss);
  177.     (*gtxt)[i] = zzz;
  178.     restofline(i,same);
  179. }
  180. char *line(int i)
  181. {
  182.     static char xxx[2]="";
  183.     static char eob[7]="[EOB]";
  184.     if (i>ngtxt) {
  185.         if (i==ngtxt+1) return &eob[0];
  186.         else return &xxx[0];
  187.     }
  188.     return (*gtxt)[i];
  189. }
  190. text_free(void)
  191. {
  192.     int i;
  193.     for (i=1;i<=ngtxt;i++) myfrees( (*gtxt)[i] ,"textfree");
  194.     ngtxt = 0;
  195. }
  196. char *tabtospace(char *s);
  197. char *sline(int i)
  198. {
  199.     char *s;
  200.     s = tabtospace(line(i));
  201.     if (strlen(s)>78) {s[78] = '>'; s[79] = 0;}
  202.     return s;
  203. }
  204. char *tabtospace(char *s)
  205. {
  206.     static char buf[255];
  207.     char *o = &buf[0];
  208.     int p=0,k,df;
  209.     for (;*s!=0;s++) {
  210.         if (*s==9) {
  211.             df = (p/8)*8+8-p;
  212.             for (k=1;k<=df;k++) {
  213.                 *o++ = ' ';
  214.                 p++;
  215.             }
  216.         } else {
  217.             *o++ = *s;
  218.             p++;
  219.         }
  220.     }
  221.     *o = 0;
  222.     return &buf[0];
  223. }
  224. text_main()
  225. {
  226.     int c,doask;
  227.     for (;;) {
  228.      doask = true;
  229.      c = text_inkey();
  230.      if (iserr) fner_clear();
  231.      switch (c) {
  232.        case eexitnow:
  233.         doask = false;
  234.        case eescape:
  235.        case equit: /* control c */
  236.         if (text_changed()) break;
  237.         if (doask) {
  238.           fner("Exit from cgle ? (Y,N)  [y]");
  239.           c = text_inkey();
  240.           fner_clear();
  241.           if (tolower(c)=='n') break;
  242.           if (tolower(c)!='y' && c != ereturn) break;
  243.         }
  244.         window(1,1,80,25);
  245.         scr_norm();
  246.         clrscr();
  247.         return;
  248.        case eedt: /* call edt editor */
  249.         text_save();
  250.         clrscr();
  251.         fner("Entering VAX EDT");
  252.         scr_refresh();
  253.         vax_edt(input_file);
  254.         text_free();
  255.         text_load(input_file);
  256.         text_setwindow();
  257.         text_refresh();
  258.         break;
  259.        case efast:
  260.         gle_speed++;
  261.         if (gle_speed>=gle_nspeed) gle_speed = 0;
  262.         sprintf(gbuff,"Speed of display set to {%d}",gle_speed);
  263.         fner(gbuff);
  264.         break;
  265.        case eleft: /* left */
  266.         text_left();
  267.         break;
  268.        case eshell: /* shell, start a subprocess */
  269.         window(1,1,80,25); scr_norm(); clrscr();
  270.         printf("Type in EXIT to return to CGLE\n\n");
  271.         system("");
  272.         text_refresh();
  273.         break;
  274.        case etrace: /* trace */
  275.         fner("Trace is now ON (or off)");
  276.         if (trace_on) trace_on = false; else trace_on = true;
  277.         break;
  278.        case eright: /* right */
  279.         text_right();
  280.         break;
  281.        case eup: /* arrow up */
  282.         text_up();
  283.         break;
  284.        case edown: /* arrow down */
  285.         text_down();
  286.         break;
  287.        case epageup:
  288.         tfy = tfy - 15;
  289.         if (tfy<1) {fner("Backup past top of file"); tfy = 1;}
  290.         text_move(tfx,tfy);
  291.         break;
  292.        case epagedown:
  293.         tfy = tfy + 15;
  294.         if (tfy>ngtxt) {fner("Advance past end of file"); tfy = ngtxt;}
  295.         text_move(tfx,tfy);
  296.         break;
  297.        case edelright:
  298.         changed = true;
  299.         text_right();
  300.         text_delete();
  301.         break;
  302.        case edeleol:
  303.         changed = true;
  304.         text_deleol();
  305.         break;
  306.        case eundeleol:
  307.         changed = true;
  308.         text_undeleol();
  309.         break;
  310.        case edelline:
  311.         changed = true;
  312.         text_deleteline();
  313.         break;
  314.       case edrawit:
  315.         window(1,1,79,24);
  316.         clrscr();
  317.         gotoxy(1,1);
  318.         scr_refresh();
  319.         gle_redraw();
  320.         if (netxt>0) text_showerror();
  321.         text_refresh();
  322.         break;
  323.       case eshowerror:
  324.         text_showerror();
  325.         text_refresh();
  326.         break;
  327.       case eundelline:
  328.         changed = true;
  329.         text_undelete();
  330.         break;
  331.       case eselect:
  332.         text_select();
  333.         break;
  334.       case ehelp: /* help */
  335.         do_help("edt");
  336.         text_refresh();
  337.         break;
  338.       case esaveas:
  339.         text_saveas();
  340.         text_refresh();
  341.         break;
  342.       case esave: /* save file */
  343.         text_save();
  344.         changed = false;
  345.         break;
  346.       case efindnext:
  347.         text_findnext();
  348.         break;
  349.       case esearch: /* search for string */
  350.         text_search();
  351.         break;
  352.       case egraphmenu: /* graph mode  */
  353.         changed = true;
  354.         menu();
  355.         text_refresh();
  356.         break;
  357.       case eload:
  358.         { char infile[80]; char wildcard[80];
  359.         if (text_changed()) break;
  360.         if (read_command(infile,"Name of file to load <return for menu> ")) break;
  361.         strcpy(wildcard,infile);
  362.         if (strlen(infile)==0) strcpy(wildcard,"*.gle");
  363.         if (strchr(wildcard,'*')!=NULL)
  364.             if (pick_file(infile,wildcard)) goto dontload;
  365.         text_free();
  366.         text_load(infile);
  367.         }
  368.         text_setwindow();
  369. dontload:    text_refresh();
  370.         break;
  371.       case ecut: /* control K , delete line, or block */
  372.         changed = true;
  373.         text_cut();
  374.         break;
  375.       case ebol:
  376.         text_move(1,tfy);
  377.         break;
  378.       case eeol:
  379.         text_move(strlen(line(tfy))+1,tfy);
  380.         break;
  381.       case eendoffile:
  382.         text_move(strlen(line(ngtxt))+1,ngtxt);
  383.         break;
  384.       case etopoffile:
  385.         text_move(1,1);
  386.         break;
  387.       case epaste: /* control Y , Yank line back, or block */
  388.         changed = true;
  389.         text_paste();
  390.         break;
  391.       case ereturn: /* carriage return */
  392.         changed = true;
  393.         text_return();
  394.         break;
  395.       case edelete: /* delete */
  396.         changed = true;
  397.         text_delete();
  398.         break;
  399.       default: /* normal key */
  400.         changed = true;
  401.         onmove = false;
  402.         if (c<26  && c!=9) {fner("Key has no affect"); break;}
  403.         if (c>200)  fner("Unimplemented command");
  404.         else normal_key(c);
  405.         break;
  406.      }
  407.     }
  408. }
  409. text_changed()
  410. {
  411.     int c;
  412.     if (!changed) return false;
  413.     for (;;) {
  414.         fner("Changes not saved,  save first ? (Y,N)");
  415.         c = text_inkey();
  416.         fner_clear();
  417.         if (c==eescape) return true;
  418.         if (tolower(c)=='n') return false;
  419.         if (tolower(c)=='y') {text_save(); return false;}
  420.     }
  421. }
  422. int text_expand(int n);
  423. text_expand(int n)
  424. {
  425.     char **a;
  426.     max_ngtxt = n;
  427.     a = myallocz((n+1)*sizeof(char *));
  428.     if (gtxt!=NULL) {
  429.         memcpy(a,gtxt,(ngtxt+1)*sizeof(char *));
  430.         myfrees(gtxt,"textexpand");
  431.     }
  432.     gtxt = (char *(*)[]) a;
  433. }
  434. text_load(char *fname)
  435. {
  436. static char inbuff[200];
  437. FILE *fptr;
  438. char *s;
  439. int i;
  440. char emess[80];
  441.     changed = false;
  442.     strcpy(input_file,fname);
  443.     s = strchr(input_file,'.');
  444.     if (s==NULL) strcat(input_file,".gle");
  445.     fptr = fopen(input_file,"r");
  446.     if (fptr==NULL) {
  447.         if (errno == ENOFILE) {
  448.         sprintf(emess,"Input file does not exist (%s) ",input_file);
  449.         fner(emess);
  450.         delay(1000);
  451.         } else {
  452.             gprint("Unable to open input file {%s} \n",input_file);
  453.             perror("Reason");
  454.             fner("Press return to continue");
  455.             text_inkey();
  456.         }
  457.     } else {
  458.      for (;!feof(fptr);) {
  459.         if (fgets(inbuff,200,fptr)!=NULL) {
  460.             i = strlen(inbuff);
  461.             if (inbuff[i-1]=='\n') inbuff[i-1] = 0;
  462.             ngtxt++;
  463.             if (ngtxt>(max_ngtxt-300)) {
  464.                 text_expand(2*(ngtxt+500));
  465.             }
  466.             (*gtxt)[ngtxt] = myalloc(i+1);
  467.             strcpy((*gtxt)[ngtxt],inbuff);
  468.         }
  469.      }
  470.      fclose(fptr);
  471.     }
  472.     if (ngtxt==0) (*gtxt)[++ngtxt] = sdup("size 24 18");
  473.     if (ngtxt==1) (*gtxt)[++ngtxt] = sdup("");
  474.     tfx = 1;
  475.     tfy = 1;
  476. }
  477. char errgle[90];
  478. int dont_clear;
  479. gle_abort(char *s)
  480. {
  481.     d_tidyup();
  482.     delay(1000);
  483.     dont_clear = true;
  484.     window(1,1,80,25);
  485.     scr_norm();
  486.     clrscr();
  487.     printf("\n\n\n\n %s \nGle is aborting, Attempting to save file in abort.gle\n",s);
  488.     strcpy(input_file,"abort.gle");
  489.     text_save();
  490.     printf("File is now saved (hopefully)\n");
  491.     if (strstr(s,"alloc")!=NULL  || strstr(s,"Init")!=NULL) {
  492.         printf("\nGLE is short of memory!!\n\n");
  493.         printf("If you are using very large datasets you should try\n");
  494.         printf("the BIGFILE option, otherwise you probably have too\n");
  495.         printf("many memory resident programs on your pc and to make GLE work\n");
  496.         printf("you should remove these from your CONFIG.SYS and AUTOEXEC.BAT\n");
  497.         printf("\n  (make coppies of these files before altering them)\n");
  498.     }
  499.     scr_end();
  500.     exit(1);
  501. }
  502. text_save()
  503. {
  504. FILE *fptr;
  505. int i;
  506.     changed = false;
  507.     fner("Saving file");
  508. #ifdef __TURBOC__
  509.     if (input_file[1] == ':') setdisk(toupper(input_file[0])-'A');
  510. #endif
  511.     unlink("glegle.bak");
  512.     unlink("glegle.tmp");
  513. #ifdef VAXC
  514. #include <file.h>
  515. {
  516. int file_desc;
  517.     file_desc = open("glegle.tmp",O_WRONLY | O_CREAT | O_TRUNC ,0,"rat=cr","rfm=var");
  518.     if (file_desc==-1) { gprint("Open glegle.tmp error \n");  return;}
  519.     fptr = fdopen(file_desc,"w");
  520. }
  521. #else
  522.     fptr = fopen("glegle.tmp","w");
  523. #endif
  524.     if (fptr==NULL) perror("Unable to open output file GLEGLE.TMP");
  525.     for (i=1;i<=ngtxt;i++) {
  526.         if (fputs(line(i),fptr)==EOF
  527.             && i<ngtxt &&  (strlen(line(i)) > 0) )
  528.             printf("Unable to write line %d {%s} \n",i,line(i));
  529.         fputs("\n",fptr);
  530.     }
  531.     if (fclose(fptr)!=0) perror("Unable to close output file GLEGLE.TMP");
  532. #ifdef __TURBOC__
  533.     if (rename(input_file,"glegle.bak")!=0) ; /*
  534.         perror("Unable to rename input file to glegle.bak"); */
  535. #endif
  536.     if (rename("glegle.tmp",input_file)!=0) perror("Unable to rename GLEGLE.TMP to output file");
  537.     fner_clear();
  538. }
  539. text_bol()
  540. {
  541.     tfx = 1;
  542.     text_move(tfx,tfy);
  543. }
  544. text_eol()
  545. {
  546.     tfx = strlen(line(tfy))+1;
  547.     text_move(tfx,tfy);
  548. }
  549. text_left()
  550. {
  551.     onmove = false;
  552.     if (tfx==1) {
  553.         if (tfy==1) return;
  554.         tfy--; tfx = strlen(line(tfy))+1;
  555.     } else tfx--;
  556.     text_move(tfx,tfy);
  557. }
  558. text_right()
  559. {
  560.     onmove = false;
  561.     if (tfx== 1+strlen(line(tfy)) ) {
  562.         if (tfy==ngtxt) return;
  563.         tfy++; tfx = 1;
  564.     } else tfx++;
  565.     text_move(tfx,tfy);
  566. }
  567. text_up()
  568. {
  569.     if (tfy==1) {fner("Backup past end of file"); return;}
  570.     if (!onmove) {
  571.         onmove = true;
  572.         move_pos = scr_tab(line(tfy),tfx);
  573.     }
  574.     tfy--;
  575.     scr_negtab(line(tfy),move_pos,&tfx,&ii);
  576.     text_move(tfx,tfy);
  577. }
  578. text_down()
  579. {
  580.     if (tfy==ngtxt) {fner("Advance past end of file"); tfy--;}
  581.     if (!onmove) {
  582.         onmove = true;
  583.         move_pos = scr_tab(line(tfy),tfx);
  584.     }
  585.     tfy++;
  586.     scr_negtab(line(tfy),move_pos,&tfx,&ii);
  587.  /*gotoxy(5,9);gprint("negtab line=%d m=%d f=%d \n",tfy,move_pos,tfx);*/
  588.     text_move(tfx,tfy);
  589. }
  590. text_return()
  591. {
  592.     strcpy(gbuff,line(tfy)+tfx-1);
  593.     ncpy(gbuff2,line(tfy),tfx-1);
  594.     lineset(tfy,gbuff2);
  595.     lineinsert(tfy+1,gbuff);
  596.     text_right();
  597. }
  598. char line_buff[255];
  599. text_undelete()
  600. {
  601.     lineinsert(tfy,line_buff);
  602.     text_move(tfx,tfy);
  603. }
  604. text_undeleol()
  605. {
  606.     strcpy(gbuff2,line(tfy)+tfx-1);
  607.     ncpy(gbuff,line(tfy),tfx-1);
  608.     strcat(gbuff,line_buff);
  609.     lineinsert(tfy,gbuff);
  610.     lineset(tfy+1,gbuff2);
  611.     text_move(tfx,tfy);
  612. }
  613. text_saveas()
  614. {
  615.     char *s;
  616.     if (read_command(input_file,"New gle file name ")) return;
  617.     s = strchr(input_file,'.');
  618.     if (s==NULL) strcat(input_file,".gle");
  619.     text_save();
  620. }
  621. char search_string[100];
  622. text_search()
  623. {
  624.     if (read_command(search_string,"Enter string to search for ")) return;
  625.     strlwr(search_string);
  626.     text_findnext();
  627. }
  628. text_findnext()
  629. {
  630.     int i;
  631.     char fline[100];
  632.     char *p;
  633.     for (i=tfy+1; i<=ngtxt; i++) {
  634.         ncpy(fline,line(i),90);
  635.         strlwr(fline);
  636.         p = strstr(fline,search_string);
  637.         if (p!=NULL) {
  638.             tfy = i; tfx = p - &fline[0] + 1;
  639.             text_move(tfx,tfy);
  640.             return;
  641.         }
  642.     }
  643.     fner("String was not found");
  644. }
  645. text_select()
  646. {
  647.     selx = tfx;    sely = tfy;
  648.     text_move(tfx,tfy);
  649. }
  650. int swapint(int *a,int *b);
  651. swapint(int *a, int *b)
  652. {
  653.     int c;
  654.     c = *a; *a = *b; *b = c;
  655. }
  656. text_cut()
  657. {
  658.     int i,x1=selx,y1=sely,x2=tfx,y2=tfy;
  659.     if (selx==0 && sely==0) return;
  660.     if (y1==y2)  if (x2<x1) swapint(&x1,&x2);
  661.     if (y2<y1) { swapint(&x1,&x2); swapint(&y1,&y2); }
  662.     if (ncut>0) {
  663.         myfrees(ctxt,"textcut");
  664.         for (i=1;i<=ncut;i++) myfree((*ctxt)[i]);
  665.     }
  666.     ctxt = myalloc((y2-y1+3)*4);
  667.     ncut = 1;
  668.     if (y1==y2) {
  669.         ncpy(gbuff,line(y1)+x1-1,x2-x1);
  670.         (*ctxt)[1] = sdup(gbuff);
  671.         ncpy(gbuff,line(y1),x1-1);
  672.         strcat(gbuff,line(y1)+x2-1);
  673.         lineset(y1,gbuff);
  674.         text_move(x1,y1);
  675.         return;
  676.     }
  677.     for (i=y1+1;i<y2;i++) (*ctxt)[++ncut] = sdup(line(i));
  678.     for (i=y1+1;i<y2;i++) linedelete(y1+1);
  679.     (*ctxt)[1] = sdup(line(y1)+x1-1);
  680.     ncpy(gbuff2,line(y1+1),x2-1);
  681.     (*ctxt)[++ncut] = sdup(gbuff2);
  682.     ncpy(gbuff,line(y1),x1-1);
  683.     strcat(gbuff,line(y1+1)+x2-1);
  684.     lineset(y1,gbuff);
  685.     linedelete(y1+1);
  686.     text_move(x1,y1);
  687. }
  688. text_paste()
  689. {
  690.     int i;
  691.     if (ncut==0) return;
  692.     if (ncut==1) {
  693.         ncpy(gbuff,line(tfy),tfx-1);
  694.         strcat(gbuff,(*ctxt)[1]);
  695.         strcat(gbuff,line(tfy)+tfx-1);
  696.         lineset(tfy,gbuff);
  697.         text_move(tfx+strlen((*ctxt)[1]),tfy);
  698.         return;
  699.     }
  700.     ncpy(gbuff,line(tfy),tfx-1);
  701.     strcat(gbuff,(*ctxt)[1]);
  702.     strcpy(gbuff2,(*ctxt)[ncut]);
  703.     strcat(gbuff2,line(tfy)+tfx-1);
  704.     lineset(tfy,gbuff);
  705.     lineinsert(tfy+1,gbuff2);
  706.     for (i=ncut-1;i>1;i--) lineinsert(tfy+1, (*ctxt)[i]);
  707.     text_move(strlen((*ctxt)[ncut])+1,tfy+ncut-1);
  708. }
  709. text_deleol()
  710. {
  711.     if (ngtxt+1==tfy) return;
  712.     strcpy(line_buff,line(tfy)+tfx-1);
  713.     ncpy(gbuff,line(tfy),tfx-1);
  714.     if (tfy<ngtxt) strcat(gbuff,line(tfy+1));
  715.     lineset(tfy,gbuff);
  716.     if (ngtxt<=tfy) {
  717.         gbuff[0] = 0;
  718.         lineinsert(tfy+1,gbuff);
  719.     } else     linedelete(tfy+1);
  720.     text_move(tfx,tfy);
  721. }
  722. text_deleteline()
  723. {
  724.     if (ngtxt==1) return;
  725.     strcpy(line_buff,line(tfy));
  726.     linedelete(tfy);
  727.     if (ngtxt<tfy) {
  728.         gbuff[0] = 0;
  729.         lineinsert(tfy,gbuff);
  730.     }
  731.     text_move(tfx,tfy);
  732. }
  733. text_delete()
  734. {
  735.     int oldx;
  736.     if (tfx==1) {
  737.         if (tfy==1) {fner("Backup past top of file"); return;}
  738.         text_left();
  739.         oldx = tfx;
  740.         strcpy(gbuff2,line(tfy));
  741.         text_deleteline();
  742.         strcat(gbuff2,line(tfy));
  743.         lineset(tfy,gbuff2);
  744.         tfx = oldx;
  745.         text_move(tfx,tfy);
  746.         return;
  747.     }
  748.     ncpy(gbuff,line(tfy),tfx-2);
  749.     strcat(gbuff,line(tfy)+tfx-1);
  750.     tfx--;
  751.     lineset(tfy,gbuff);
  752.     text_move(tfx,tfy);
  753. }
  754. text_setwindow()
  755. {
  756.     if (tfy>w_top-4 || tfy<w_bot+4) w_top = tfy - (w_len-1)/2;
  757.     if (w_top<1) w_top = 1;
  758.     w_bot = w_top + w_len - 1;
  759. }
  760. text_refresh()
  761. {
  762.     char doubleline[90];
  763.     int e,i;
  764.     iserr = true; /* so that function keys appear at bottom */
  765.     window(1,1,80,25);
  766.     scr_norm();
  767.     clrscr();
  768.     gotoxy(1,1); wprintf("Memory Used=%ld  Free=%ld   CGLE V3.2f  Current file={%s}"
  769.         ,mem_total(),coreleft(),input_file);
  770.  
  771.     memset(doubleline,HLINECHAR,80);
  772.     doubleline[78] = 0;
  773.     gotoxy(1,2); cputs(doubleline);
  774.  
  775.     fner_clear();
  776.     window(2,3,79,23);
  777.     gotoxy(1,1);
  778.     e = w_bot;
  779.     if (e>ngtxt) e = ngtxt;
  780.     for (i=w_top;i<=e;i++) {
  781.         gotoxy(1,i-w_top+1);
  782.         cputs( sline(i) );
  783.     }
  784.     if (e<w_bot) {
  785.         gotoxy(1,i-w_top+1);
  786.         cputs("[EOF]");
  787.     }
  788.     gotoxy(scr_tab(line(tfy),tfx), tfy - w_top + 1);
  789. }
  790. /*---------- move cursor to file coloumn x, line y */
  791. text_move(int x,int y)
  792. {
  793.     tfx = x;
  794.     tfy = y;
  795.     if (tfy>ngtxt) tfy = ngtxt;
  796.     if (tfy<1) tfy = 1;
  797. /*    if (tfx>79) tfx = 79; */
  798.     if (tfx>strlen(line(tfy))+1) tfx = strlen(line(tfy))+1;
  799.     if (tfx<1) tfx = 1;
  800.     if (tfy<(w_top+5) || tfy>(w_bot-5)) {
  801.         if (tfy<(w_top-25) || tfy>(w_bot+25)) {
  802.             text_setwindow();
  803.             text_refresh();
  804.         } else {
  805.             text_scroll();
  806.         }
  807.     }
  808.     gotoxy(scr_tab(line(tfy),tfx), tfy - w_top + 1);
  809. }
  810. restofline(int y,int x)
  811. {
  812.     int sx;
  813.     if (x <= strlen(line(y))) {
  814.         sx = scr_tab(line(y),x);
  815.         gotoxy(sx,y-w_top + 1);
  816.         clreol();
  817.         cputs(sline(y)+sx-1);
  818.         gotoxy(scr_tab(line(y),tfx), y - w_top + 1);
  819.     }
  820. }
  821. fixline(int y)
  822. {
  823.     gotoxy(1,y-w_top + 1);
  824.     clreol();
  825.     cputs(sline(y));
  826. }
  827. scrinsert(int y)
  828. {
  829.     gotoxy(1,y-w_top + 1);
  830.     insline();
  831.     cputs(sline(y));
  832. }
  833. text_scroll()
  834. {
  835.     int n,i;
  836.     n = w_top - tfy + 5;
  837.     /* printf("n %d, w_top %d  tfy %d \n",n,w_top,tfy); */
  838.     if (w_top-n<1) n = w_top - 1;
  839.     if (n>0) {
  840.         for (i=0;i<n;i++) text_scroll_down();
  841.         return;
  842.     }
  843.     n = tfy - w_bot + 5;
  844.     if (w_bot+n>ngtxt) n = ngtxt - w_bot;
  845.     if (n>0) {
  846.         for (i=0;i<n;i++) text_scroll_up();
  847.         return;
  848.     }
  849. }
  850. /* ------------------------ Find screen position given x pos in string */
  851. scr_tab(char *s, int x)
  852. {
  853.     int p=0,i=0;
  854.     for (;*s!=0 && i<(x-1);i++,s++) {
  855.         if (*s==9)  p = (p/8)*8+8;
  856.         else       p++;
  857.     }
  858.     return ++p;
  859. }
  860. /*------------------------- Find file x position given x pos on screen */
  861. /* and sets scrx to the correct screen position (if in middle of tab) */
  862. scr_negtab(char *s, int x, int *fpos, int *scrx)
  863. {
  864.     int last_pos=0,i;
  865.     *fpos=0;
  866.     *scrx = 0;
  867.  
  868.     for (i=0;*s!=0;s++,i++) {
  869.         if (*s==9) *scrx = (*scrx/8)*8+8;
  870.         else (*scrx)++;
  871.         if ((*scrx+1)>x) {
  872.             *fpos = i+1;
  873.             *scrx = last_pos;
  874.             return;
  875.         }
  876.         last_pos = *scrx + 1;
  877.     }
  878.     if (*scrx==0) (*scrx)++;
  879.     *fpos = i+1;
  880.     if (i==0) *fpos = 1;
  881. }
  882. text_scroll_down()
  883. {
  884.     gotoxy(1,1);insline();
  885.     w_top--;w_bot--;
  886.     fixline(w_top);
  887. }
  888. text_scroll_up()
  889. {
  890.     char ss[90];
  891.     gotoxy(1,1); delline();
  892.     w_top++; w_bot++;
  893.     fixline(w_bot);
  894. }
  895. scrdeleteline(int y)
  896. {
  897.     gotoxy(1, y - w_top + 1);
  898.     delline();
  899.     fixline(w_bot);
  900. }
  901. extern int noscreenio;
  902. char *function_bar(void);
  903. fner_clear()
  904. {
  905.     if (noscreenio) return;
  906.     if (iserr==false) return;
  907.     if (dont_clear) return;
  908.     window(1,1,80,25);
  909.     gotoxy(1,25);
  910.     scr_grey();
  911.     clreol();
  912.     gotoxy(2,25);
  913.     cputs(function_bar());
  914.     scr_norm();
  915.     iserr = false;
  916.     window(2,3,79,23);
  917. }
  918. extern int noscreenio;
  919. fner(char *s)
  920. {
  921.     if (dont_clear) return;
  922.     if (noscreenio) {
  923.         printf("%s\n",s);
  924.         return;
  925.     }
  926.     scr_savexy();
  927.     iserr = true;
  928.     window(1,1,80,25);
  929.     gotoxy(1,25);
  930.     scr_inv();
  931.     clreol();
  932.     gotoxy(2,25);
  933.     cputs(s);
  934.     scr_norm();
  935.     scr_restorexy();
  936.     scr_refresh();
  937. }
  938. int read_command(char *ans,char *ques)
  939. {
  940.     int rr;
  941.     iserr = true;
  942.     window(1,1,80,25);
  943.     gotoxy(1,24);
  944.     scr_inv();
  945.     clreol();
  946.     gotoxy(2,24);
  947.     cputs(ques);
  948.     rr = read_str(ans);
  949.     gotoxy(1,24);
  950.     scr_norm();
  951.     clreol();
  952.     window(2,3,79,23);
  953.     text_move(tfx,tfy);
  954.     return rr;
  955. }
  956. int read_input(char *ans,char *ques)
  957. {
  958.     int rr;
  959.     iserr = true;
  960.     clreol();
  961.     cputs(ques);
  962.     rr = read_str(ans);
  963.     wprintf("\n");
  964.     return rr;
  965. }
  966.  
  967. int read_str(char *s)
  968. {
  969.     int c,cx=0;
  970.     char mbuff[80];
  971.     *s = 0;
  972.     for (;;) {
  973.      c = text_inkey();
  974.      switch (c) {
  975.        case eescape: /* ESCAPE */
  976.        case equit: /* control c */
  977.         return true;
  978.        case eleft: /* left */
  979.         if (cx <= 0) break;
  980.         cx--;
  981.         scr_left(1);
  982.         break;
  983.        case eright: /* right */
  984.         if (cx >= strlen(s)) break;
  985.         cx++;
  986.         scr_right(1);
  987.         break;
  988.        case eup: /* arrow up */
  989.        case edown: /* arrow down */
  990.       case ehelp: /* f1 help */
  991.         break;
  992.       case ereturn: /* carriage return */
  993.         return false;
  994.       case edelete: /* delete */
  995.         if (strlen(s)==0) break;
  996.         if (cx<1) break;
  997.         ncpy(mbuff,s,cx-1);
  998.         strcat(mbuff,s + cx);
  999.         strcpy(s,mbuff);
  1000.         cx--;
  1001.         scr_left(1);
  1002.         cputs(s + cx);
  1003.         putch(' ');
  1004.         scr_left(strlen(s+cx)+1);
  1005.         break;
  1006.       case eshowerror:
  1007.       case edrawit:
  1008.         break;
  1009.       default: /* normal key */
  1010.         if (c<26  && c!=9) {fner("Key has no affect"); break;}
  1011.         if (c>200)  fner("Unimplemented command");
  1012.         else {
  1013.             ncpy(mbuff,s,cx);
  1014.             mbuff[cx] = c; mbuff[cx+1] = 0;
  1015.             strcat(mbuff,s + cx);
  1016.             strcpy(s,mbuff);
  1017.             cputs(s + cx);
  1018.             cx++;
  1019.             scr_left(strlen(s+cx));
  1020.         }
  1021.         break;
  1022.        }
  1023.      }
  1024. }
  1025. normal_key(int c)
  1026. {
  1027.     static char lbuff[255];
  1028.     if (ngtxt+1==tfy) {
  1029.         text_up();
  1030.         text_eol();
  1031.         text_return();
  1032.     }
  1033.     ncpy(lbuff,line(tfy),tfx-1);
  1034.     lbuff[tfx-1] = c; lbuff[tfx]=0;
  1035.     strcat(lbuff,line(tfy)+tfx-1);
  1036.     tfx++;
  1037.     lineset(tfy,lbuff);
  1038. }
  1039. text_showerror()
  1040. {
  1041.     int i,n;
  1042.     if (netxt==0) {
  1043.         fner("There were no errors, press return");
  1044.         text_inkey();
  1045.         return;
  1046.     }
  1047.     clrscr();
  1048.     n = netxt;
  1049.     if (n>20) n = 17;
  1050.     for (i=1;i<=n;i++) {
  1051.         gotoxy(3,i);
  1052.         cputs( (*etxt)[i] );
  1053.     }
  1054.     gotoxy(1,20); cputs("Press any key to continue");
  1055.     text_inkey();
  1056. }
  1057. #ifndef unix
  1058. void wprintf(va_list arg_list, ...)
  1059. /* Prints to the window */
  1060. {
  1061.      va_list arg_ptr;
  1062.      char *format;
  1063.     char output[200];
  1064.  
  1065.      va_start(arg_ptr, arg_list);
  1066.      format = arg_list;
  1067.      vsprintf(output, format, arg_ptr);
  1068. #ifdef __TURBOC__
  1069.     printf(output);
  1070. #else
  1071.     cputs(output);
  1072. #endif
  1073. }
  1074. #endif
  1075.  
  1076.